<template>
	<view style="max-height:300px;">
		<tree-com :tree-data="treeData" :ready="ready" :show-btn="showBtn" :showRadio="showRadio" node-key="id"
			:props="props" lazy :load="loadNode" @node-expand="handleNodeExpand" @node-click="handleNodeClick"
			@edit-name="handleEditName" @check="handleCheckChange"></tree-com>
	</view>
</template>

<script>
	import treeCom from '../../../../components/ly-tree-nvue/ly-tree'
	import {
		regionalRoot,
		getSubOrgList,
	} from '../../../../api/eqment/index.js'
	export default {
		components: {
			treeCom
		},
		props: {
			showBtn: {
				type: Boolean,
				default: false
			},
			showRadio: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				ready: true,
				props: {
					label: 'orgName',
				},
				clickedId: '',
				treeData: []
			}
		},
		created() {
			this.getData()
		},
		methods: {
			loadNode(node, resolve) {
				console.log(node.level)
				if (node.level === 0) {
					return resolve(this.treeData);
				}
				// if (!this.clickedId) return resolve([]);
				setTimeout(() => {
					console.log(this.clickedId)
					//获取树的子节点
					getSubOrgList({
						pid: this.clickedId
					}).then((res) => {
						console.log(res)
						resolve(res.datas);
					})
				}, 500);
				
			},
			//获取数据
			getData() {
				regionalRoot({

				}).then((res) => {
					let dataArr = new Array();
					dataArr[0] = res.expandData.root;
					dataArr[0]['children'] = res.expandData.childrens;
					this.treeData = dataArr;
					this.clickedId = this.treeData[0].id;
				})
			},
			handleNodeClick(obj) {
				// console.log('handleNodeClick', obj);
				this.clickedId = obj.data.id
			},
			handleNodeExpand(obj) {
				// console.log('handleNodeExpand', JSON.stringify(obj));
				this.clickedId = obj.data.id
			},
			handleEditName(obj) {
				this.$emit('click-edit-name', obj)
			},
			handleCheckChange(obj) {
				this.clickedId = obj.data.id
				this.$emit('handleRadioTree',obj)
			}
		}
	}
</script>

<style>

</style>
